import os
from os.path import join, exists
from uuid import uuid4

from Autotests.BaseTest import AirClientTestCase
from Autotests.LocalMachine.Pages.AirClient.BackupPage import WebBackupPage, BackupPlugins
from Autotests.LocalMachine.Pages.AirClient.RestorePage import WebRestorePage


class Restore(AirClientTestCase):
    def setUp(self):
        super(Restore, self).setUp()
        self.fileForBackup1 = join(os.getcwd(), "TEST_ONE.txt")
        self.backupFileContent1 = 1024 * 1024 * str(uuid4())
        with open(self.fileForBackup1, 'wb') as f:
            f.write(self.backupFileContent1)

        self.fileForBackup2 = join(os.getcwd(), "TEST_TWO.txt")
        self.backupFileContent2 = 1024 * 32 * str(uuid4())
        with open(self.fileForBackup2, 'wb') as f:
            f.write(self.backupFileContent2)

        self.backup = WebBackupPage()
        self.restore = WebRestorePage()

    def test_Search(self):
        self.backup.performBackup(self.fileForBackup1, BackupPlugins.filesystem)
        self.backup.performBackup(self.fileForBackup2, BackupPlugins.filesystem)

        self.restore.performSearch('Test')
        self.assertTrue(self.restore.getSearchResultRowCount() >= 2, "Search failed")
        self.assertTrue(self.restore.getLastBackupDate() in self.restore.getSearchResultColumn(1),
                        "Search results are incorrect: no file with latest backup date found")

        self.restore.performSearch('TWO')
        self.assertTrue(self.restore.getSearchResultRowCount() >= 1, "Search failed")
        self.assertIn('TWO'.lower(), self.restore.getSearchResultColumn(0)[0].lower(),
                      "Search results are incorrect: found files don't match search criteria")
        self.assertTrue(self.restore.getLastBackupDate() in self.restore.getSearchResultColumn(1),
                        "Search results are incorrect: no file with latest backup date found")

    def tearDownImpl(self):
        if exists(self.fileForBackup1):
            os.remove(self.fileForBackup1)
        if exists(self.fileForBackup2):
            os.remove(self.fileForBackup2)

import unittest
if __name__ == "__main__":
    unittest.main()

